package com.epam.weblibrary.controllers;

import com.epam.weblibrary.entities.book.Book;
import com.epam.weblibrary.entities.user.Roles;
import com.epam.weblibrary.entities.user.User;
import com.epam.weblibrary.service.book.BookService;
import com.epam.weblibrary.service.order.OrderService;
import com.epam.weblibrary.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;

import javax.validation.Valid;

/**
 * Created by DruidKuma on 9/24/14.
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private BookService bookService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(byte[].class,
                new ByteArrayMultipartFileEditor());
    }

    @RequestMapping(value = "/panel", method = RequestMethod.GET)
    public String getControlPanel(Model model) {
        model.addAttribute("users", userService.findAll());
        model.addAttribute("orders", orderService.findAll());
        return "admin/panel";
    }

    @RequestMapping(value = "/user", params = "remove")
    public String removeUser(@RequestParam Long id) {
        userService.delete(id);
        return "redirect:panel";
    }

    @RequestMapping(value = "/book", params = "add", method = RequestMethod.GET)
    public String getAddBook(@ModelAttribute Book book, BindingResult result) {
        return "book/add";
    }

    @RequestMapping(value = "/book", params = "add", method = RequestMethod.POST, consumes = {"multipart/form-data"})
    public String postAddContact(@ModelAttribute @Valid Book book, BindingResult result) {
        if(result.hasErrors()) {
            return "book/add";
        }
        book = bookService.save(book);
        return "redirect:/book?id=" + book.getId();
    }

    @RequestMapping(value = "/book", params = "edit", method = RequestMethod.GET)
    public String getEditContact(@RequestParam Long id, @ModelAttribute Book book, BindingResult result, Model model) {
        model.addAttribute("book", bookService.find(id));
        return "book/edit";
    }

    @RequestMapping(value = "/book", params = "edit", method = RequestMethod.POST)
    public String postEditContact(@ModelAttribute @Valid Book book, BindingResult result) {
        Book oldBook = bookService.find(book.getId());
        book.setCover(oldBook.getCover());
        if(result.getAllErrors().size() > 1) {
            return "book/edit";
        }

        bookService.save(book);
        return "redirect:/book?id=" + book.getId();
    }

    @RequestMapping(value = "/book", params = "delete", method = RequestMethod.POST)
    public String postDeleteContact(@RequestParam Long id) {
        bookService.delete(id);
        return "redirect:/books/1";
    }

    @RequestMapping(value = "/user", params = "discount", method = RequestMethod.POST)
    public String updateUserDiscount(@RequestParam Long id, @RequestParam Long newDiscount) {
        if(newDiscount != null) {
            User user = userService.find(id);
            user.setDiscount((float) (newDiscount/100.));
            userService.save(user);
        }
        return "redirect:panel";
    }

    @RequestMapping(value = "/user", params = "role", method = RequestMethod.POST)
    public String updateUserRole(@RequestParam Long id, @RequestParam String newRole) {
        User user = userService.find(id);
        if(newRole.equals("ROLE_ADMIN")) {
            user.setRole(Roles.ROLE_ADMIN);
        } else {
            user.setRole(Roles.ROLE_USER);
        }
        userService.save(user);
        return "redirect:panel";
    }
}
